<?php
/*
 * @分销商城
 * @功能说明：用户管理中心
 * @更新说明：暂无更新
 * @文件名 UserController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-8 下午2:06:50
 * @创建人 XING
 */

namespace wap\Controller;
use Think\Controller;
require SCRIPT_ROOT.'/../../library/wx/wxBizMsgCrypt.php';

class UserController extends Controller
{
    /*
	 * 分销商城_登陆
	 * @date: 2016-3-9 下午2:11:14
	 * @editor: XING
	 */
	public function login(){
		if(I('get.code')){
			session(array('expire'=>7200));
			$this->assign('msg','非法身份信息');
			$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx96f4e32fe927be19&secret=edfdb7056ff5fac9a74897c7256b6b25&code='.I('get.code').'&grant_type=authorization_code';
			if(S('wx'.I('get.code'))){
				$arrBack = S('wx'.I('get.code'));
			}else{
				$arrBack = json_decode(file_get_contents($url),1);
				S('wx'.I('get.code'),$arrBack,7000);
			}
			if(isset($arrBack['access_token']) && $arrBack['access_token'] && $arrBack['openid']){
				$userToken = json_decode(file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$arrBack['access_token'].'&openid='.$arrBack['openid'].'&lang=zh_CN'),1);
				if($userToken && isset($userToken['headimgurl'])){
					$checkExist = M('CustEntity')
							    ->where(array('wxsek'=>$arrBack['openid']))->find();
					if($checkExist){
						session('loginUid',$checkExist['cust_id']);
						session('loginMobile',$checkExist['cust_mobile']);
						session('loginName',$checkExist['cust_realname']);
						session('loginNickname',$checkExist['nickname']);
						session('wxopenid'.$checkExist['cust_id'],$arrBack['openid']);
						session('access_token',$arrBack['access_token']);
						session('loginImg',$userToken['headimgurl']);
						
						redirect(I('get.refUrl') ?: '/');
					}else{
						$putInServer = M('CustEntity')->add(array(
									'wxsek' => $arrBack['openid'],
									'cust_img' => $userToken['headimgurl'],
									'nickname' => $userToken['nickname'],
									'is_check' => '-2',
									'salelevel' => '0',
									'add_at' => time(),
									'update_at' => time()
								));
						if($putInServer){
							session('loginUid',$putInServer);
							session('loginMobile','');
							session('loginName','');
							session('loginNickname',$checkExist['nickname']);
							session('wxopenid'.$putInServer,$arrBack['openid']);
							session('access_token',$arrBack['access_token']);
							session('loginImg',$userToken['headimgurl']);
							redirect(I('get.refUrl') ?: '/');
						}else{
							$this->assign('msg','身份信息数据错误，请重试！');
							$this->display('saler/noauth');
						}
					}
				}else{
					$this->display('saler/noauth');
				}
			}else{
				$this->assign('msg','非法身份信息11');
				$this->display('saler/noauth');
			}
		}else{
			$this->assign('msg','非法身份信息233');
			$this->display('saler/noauth');
		}
		exit;
	}
	
    /*
	 * 分销商城_服务器验证
	 * @date: 2016-3-22 下午3:01:17
	 * @editor: XING
	 */
	public function authCheck(){
		//echo (new \service\wx\Wchat())->valid();exit;
		$postData = file_get_contents('php://input');
		if($postData){
			// 第三方收到公众号平台发送的消息
			$pc = new \WXBizMsgCrypt('805a3t45a234a', 'FjLGkLMoggAP2reW2FHz8zBvpsmst0YDfnfzfo7H06k', 'wx96f4e32fe927be19');
			$msg_sign = I('get.msg_signature') ?: 'signature';
			$msg = '';$nonce = I('get.nonce') ?: 'hahehahe123';
			$errCode = $pc->decryptMsg($msg_sign, time(), $nonce, $postData, $msg);
			$msgArr = xmlToArray($msg);
			if($msgArr && $msgArr['EventKey']){
				switch($msgArr['EventKey']){
					case 'spreaduser':
						$getCustMain = M('CustEntity')->field('cust_id,salelevel')
								   ->where(array('wxsek'=>$msgArr['FromUserName']))->find();
						if($getCustMain['salelevel'] > 0){
							$requestUrl = 'http://wap.hipaygo.cn/goods/glist/oem/1/sek/'.mySpreadSek($getCustMain['cust_id']);
						//	$tinyurl = file_get_contents('http://6du.in/?is_api=1&lurl='.$requestUrl);
							$tinyurl = '';
							if($tinyurl){
								$requestUrl = $tinyurl;
							}
							$tips = '您的专属推广链接 '.$requestUrl;
						}else{
							$tips = '抱歉，您需要升级为卖家才能获取推广链接~';
						}
						echo '<xml>
<ToUserName><![CDATA['.$msgArr['FromUserName'].']]></ToUserName>
<FromUserName><![CDATA[hipaygo]]></FromUserName>
<CreateTime>'.time().'</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA['.$tips.']]></Content>
</xml>';exit;
						break;
						
					case 'contact':
							echo '<xml>
<ToUserName><![CDATA['.$msgArr['FromUserName'].']]></ToUserName>
<FromUserName><![CDATA[hipaygo]]></FromUserName>
<CreateTime>'.time().'</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[客服电话：0571-81389134、0571-81389138，或在公众号内直接回复问题。工作时间周一到周日: 9点-20点]]></Content>
</xml>';exit;
							break;
				}
				
			}else{
				echo '<xml>
     <ToUserName><![CDATA['.$msgArr['FromUserName'].']]></ToUserName>
     <FromUserName><![CDATA[hipaygo]]></FromUserName>
     <CreateTime>'.time().'</CreateTime>
     <MsgType><![CDATA[transfer_customer_service]]></MsgType>
 </xml>';exit;
			}
				
			
		}
		
		
		
		
	}
	
    /*
	 * 分销商城_设置菜单
	 * @date: 2016-3-22 下午3:44:13
	 * @editor: XING
	 */
	public function menuCreate(){
		$data = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx96f4e32fe927be19&secret=edfdb7056ff5fac9a74897c7256b6b25');
		$rebackJson = json_decode($data,1);
		$token = $rebackJson['access_token'];
		$arr = array(
				'button' => array(
							array(
									'type' => 'view',
									'name' => urlencode('我是买家'),
									'url' => 'http://wap.hipaygo.cn/'
								),
							array(
									'name' => urlencode('我是卖家'),
									'sub_button' => array(
												array(
														'type' => 'view',
														'name' => urlencode('卖家中心'),
														'url' => 'http://wap.hipaygo.cn/saler/index/'	
													),
												array(
														'type' => 'click',
														'name' => urlencode('推广代理'),
														'key' => 'spreaduser'	
													)
											)	
								),
							array(
									'name' => urlencode('客服中心'),
									'sub_button' => array(
											array(
													'type' => 'view',
													'name' => urlencode('视频介绍'),
													'url' => 'http://mp.weixin.qq.com/s?__biz=MzI0MzI1MDQzNw==&mid=100000002&idx=1&sn=27e3f63db9cae81e09c14857e19c412e#rd'
											),
											array(
													'type' => 'view',
													'name' => urlencode('加入指南'),
													'url' => 'http://mp.weixin.qq.com/s?__biz=MzI0MzI1MDQzNw==&mid=100000023&idx=1&sn=04d766ad38b35a83a29a4d908eb2fe4e#rd'
												),
											array(
												'type' => 'click',
												'name' => urlencode('联系客服'),
												'key' => 'contact'
											)
										)
										
								)
						)
				);
		$dataBack = curl('https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token,urldecode(json_encode($arr)));

		print_R($dataBack);exit;
	}
	
	/*
	 * 分销商城_买家用户中心
	 * @date: 2016-3-15 下午3:58:42
	 * @editor: XING
	 */
	public function index(){
		if(session('loginUid')){
			$this->display('index/user');
		}else{
			$this->assign('msg','身份信息数据错误，请重试！[1008]');
			$this->display('saler/noauth');
		}
	}
}
